package com.example.android_wx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;

import com.example.android_wx.R;
import com.example.android_wx.base.App;
import com.example.android_wx.base.BaseActivity;
import com.example.android_wx.http.BaseCallBack;
import com.example.android_wx.http.HttpHelp;
import com.example.android_wx.http.MemberData;
import com.google.gson.Gson;


public class LoginActivity extends BaseActivity {

    private AppCompatEditText mobileEditText;
    private AppCompatEditText passwordEditText;
    private AppCompatTextView loginTextView;
    private AppCompatTextView registerTextView, retrieveTextView;
    private View mobileView, passwordView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        super.onCreate(savedInstanceState);
        String token = App.get().getSharedPreferences().getString("token", "");
        if (!token.isEmpty()) getMemberData();
    }


    @Override
    public void initView() {

        mobileEditText = findViewById(R.id.mobileEditText);
        passwordEditText = findViewById(R.id.passwordEditText);
        loginTextView = findViewById(R.id.loginTextView);
        registerTextView = findViewById(R.id.registerTextView);
        retrieveTextView = findViewById(R.id.retrieveTextView);
        mobileView = findViewById(R.id.mobileView);
        passwordView = findViewById(R.id.passwordView);


        DialogActivity dig = new DialogActivity(this);
        dig.show();


    }

    @Override
    public void initEvent() {

        loginTextView.setOnClickListener(view -> {
            String mobile = mobileEditText.getText().toString();
            String password = passwordEditText.getText().toString();
            if (mobile.isEmpty()) {
                Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                return;
            }
            if (password.isEmpty()) {
                Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                return;
            }
            //防止多次点击
            loginTextView.setEnabled(false);//禁用按钮
            loginTextView.setText("正在登录...");//修改按钮文字
            //发送请求  15207713074 123456
            HttpHelp.get().ready("base", "login")
                    .add("mobile", mobile)
                    .add("password", password)
                    .get(new BaseCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            loginTextView.setEnabled(true);// 启用按钮
                            loginTextView.setText("登录");//修改按钮文字

                            //手动更新httpHelp让他带上token
                            HttpHelp.get().setToken(data);

                            App.get().getEditor().putString("token", data);
                            App.get().getEditor().commit();
                            getMemberData();

                        }

                        @Override
                        public void onFailure(String reason) {
                            Toast.makeText(LoginActivity.this, reason, Toast.LENGTH_SHORT).show();
                            loginTextView.setEnabled(true);// 启用按钮
                            loginTextView.setText("登录");//修改按钮文字
                        }
                    });
        });

        mobileEditText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                mobileView.setBackgroundResource(R.color.primary);
                passwordView.setBackgroundResource(R.color.textHint);
            }
        });

        passwordEditText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                mobileView.setBackgroundResource(R.color.textHint);
                passwordView.setBackgroundResource(R.color.primary);
            }
        });

        registerTextView.setOnClickListener(view -> {
            //activity之间的跳转 使用intent，一定要确保目标activity已经在manifests文件注册
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            //数据回传
            startActivityForResult(intent, 999);
        });

        //找回密码
        retrieveTextView.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this, RetrieveActivity.class);
            startActivity(intent);
        });


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1 && requestCode == 1) {
            String username = data.getStringExtra("mobile");
            mobileEditText.setText(username);
        }
    }

    private void getMemberData() {

        HttpHelp.get().ready("member", "getMemberData")
                .get(new BaseCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        App.get().setMemberData(new Gson().fromJson(data, MemberData.class));
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        finish();
                    }

                    @Override
                    public void onFailure(String reason) {
                        if (reason.contains("登录")) {
                            Toast.makeText(LoginActivity.this, "请重新登录", Toast.LENGTH_SHORT).show();
                            App.get().setMemberData(null);
                            App.get().getEditor().putString("token", "");
                            App.get().getEditor().commit();
                        }


                    }
                });

    }

}
